package com.imay.live.view.custom;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.imay.base.util.ViewUtil;
import com.imay.live.R;
import com.imay.live.manager.GameLiveViewAnimManager;
import com.imay.live.manager.GuideManager;
import com.imay.live.model.RoomInfoModel;

import java.util.List;

/**
 * Created by jeaman on 2017/3/24.
 * 直播UI
 */

public class RoomLiveNormalView extends RoomBaseView {
    private RoomRecommendView mRoomRecommendView;
    private View mHideRootView; //右滑隐藏view
    private View mActionRightRootView;  //右滑不隐藏的view
    private ImageView mFullScreenView;
    private View mUserGameView;
//    private View mUserMicView;
    private View mCharmView;
    private View mWatermarkView;
    private View mRecordView;

    private OnLiveActionListener mListener;
    private boolean mSimpleMode;
    private boolean mHideAllView;   //未进入房间前，隐藏所有view
    private boolean mHorizontalMode;

    public RoomLiveNormalView(Context context) {
        super(context);
    }

    public RoomLiveNormalView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoomLiveNormalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RoomLiveNormalView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    protected int getLayoutId() {
        return R.layout.view_room_live_normal;
    }

    protected void init(Context context) {
        super.init(context);
        mTopRootView = findViewById(R.id.live_room_rl_top);
        mActionRootView = findViewById(R.id.live_room_fl_action);
        mHideRootView = findViewById(R.id.live_room_rl_hide);
        mActionRightRootView = findViewById(R.id.live_room_rl_action_right);
        mFullScreenView = (ImageView) findViewById(R.id.live_room_iv_full);
        mUserGameView = findViewById(R.id.live_room_iv_game);
//        mUserMicView = findViewById(R.id.live_room_iv_mic);
        mCharmView = findViewById(R.id.live_room_ll_charm);
        mWatermarkView = findViewById(R.id.live_room_ll_watermark);
        mRecordView = findViewById(R.id.live_room_iv_record);
        mGiftRedPointView = findViewById(R.id.live_room_iv_gift_red_point);

        //直播结束页面在公屏区下方
        mRoomRecommendView = new RoomRecommendView(context);
        mChatRootView.addView(mRoomRecommendView, 0);

        initListener();
    }

    protected void initListener() {
//        super.initListener();
//        findViewById(R.id.live_room_iv_mic).setOnClickListener(this);
        findViewById(R.id.live_room_iv_game).setOnClickListener(this);
        findViewById(R.id.live_room_iv_gift).setOnClickListener(this);
        findViewById(R.id.live_room_iv_record).setOnClickListener(this);
        findViewById(R.id.live_room_iv_screenshot).setOnClickListener(this);
        findViewById(R.id.live_room_iv_share).setOnClickListener(this);
        findViewById(R.id.live_room_iv_full).setOnClickListener(this);

        mRoomRecommendView.setOnRoomRecommendClickListener(new RoomRecommendView.OnRoomRecommendClickListener() {
            @Override
            public void onRecommendChangeClick() {
                if (mListener != null) {
                    mListener.onRecommendChangeClick();
                }
            }

            @Override
            public void onRecommendRoomClick(RoomInfoModel room) {
                if (mListener != null) {
                    mListener.onRecommendRoomClick(room);
                }
            }
        });
    }

    //滑动隐藏view
    @Override
    public boolean toggleSimpleMode(boolean simple) {
        if (mSimpleMode == simple) {
            return false;
        }
        mSimpleMode = simple;
        int visibility = simple ? INVISIBLE : VISIBLE;
        com.imay.live.utils.ViewUtil.startVisibilityAnimation(mHideRootView, visibility);

        return true;
    }

    //隐藏除关闭按钮外的所有view
    public void hideAllView() {
        mHideAllView = true;
        toggleSimpleMode(true);
        com.imay.live.utils.ViewUtil.startVisibilityAnimation(mActionRightRootView, INVISIBLE);
    }

    public void showAllView() {
        mHideAllView = false;
        toggleSimpleMode(false);
        com.imay.live.utils.ViewUtil.startVisibilityAnimation(mActionRightRootView, VISIBLE);
    }

    @Override
    public boolean isHideAllView() {
        return mHideAllView;
    }

    public void setRecommendRoom(List<RoomInfoModel> data) {
        mRoomRecommendView.setRoomData(data);
        mRoomRecommendView.setVisibility(VISIBLE);
    }

    public void hideRoomRecommendView() {
        mRoomRecommendView.setVisibility(GONE);
    }

    public void changeToGameViewVertical(boolean isHorizontalPush) {
        setBottomLayoutVisible(true);
        setHorizontalMode(false);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mFullScreenView.getLayoutParams());
        mFullScreenView.setImageResource(R.mipmap.btn_03z_fangda);
        if (isHorizontalPush){
            layoutParams.setMargins(0,(int)ViewUtil.dip2px(getContext(),292),0,0);
            mFullScreenView.setVisibility(View.VISIBLE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        }else{
            layoutParams.setMargins(0,0,0,0);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
            mFullScreenView.setVisibility(View.GONE);
        }
        mFullScreenView.setLayoutParams(layoutParams);

    }

    public void changeToGameViewHorizontal() {
        setBottomLayoutVisible(false);
        setHorizontalMode(true);
        mFullScreenView.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mFullScreenView.getLayoutParams());
        layoutParams.setMargins(0,0,0,0);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
        mFullScreenView.setLayoutParams(layoutParams);
        mFullScreenView.setImageResource(R.mipmap.btn_03z_suoxiao);
    }

    public void changeToNormal() {
        setBottomLayoutVisible(true);
        setHorizontalMode(false);
        mFullScreenView.setVisibility(View.GONE);
    }

    public void setFullScreenVisibility(boolean visible) {
        mFullScreenView.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setViewStatusByMode(boolean visible) {
        mUserGameView.setVisibility(visible ? VISIBLE : GONE);
//        mUserMicView.setVisibility(visible ? VISIBLE : GONE);
        mRecordView.setVisibility(visible ? VISIBLE : GONE);
        findViewById(R.id.live_room_iv_screenshot).setVisibility(visible ? VISIBLE : GONE);

    }

 

    public void setBottomLayoutVisible(boolean visible){
        mActionRootView.setVisibility(visible ? VISIBLE : GONE);
        mActionRightRootView.setVisibility(visible ? VISIBLE : GONE);
        mUserRecyclerView.setVisibility(visible ? VISIBLE : GONE);
        mCharmView.setVisibility(visible ? VISIBLE : GONE);
        mWatermarkView.setVisibility(visible ? VISIBLE : GONE);
        mChatRootView.setVisibility(visible ? VISIBLE : GONE);
//        mCloseView.setVisibility(visible ? VISIBLE : GONE);
    }

    // 横屏状态下设置View 的状态
    public void setPortraitViewVisible(boolean visible){
        if (visible){
            GameLiveViewAnimManager.getInstance().startUserInfoViewShowAnim(mTopRootView);
            GameLiveViewAnimManager.getInstance().startSwitchViewShowAnim(mFullScreenView);
        }else{
            GameLiveViewAnimManager.getInstance().startUserInfoViewHideAnim(mTopRootView);
            GameLiveViewAnimManager.getInstance().startSwitchViewHideAnim(mFullScreenView);
        }
    }

    public void setHorizontalMode(boolean horizontalMode) {
        mHorizontalMode = horizontalMode;
    }

    @Override
    public void setActionViewVisibility(boolean visible) {
        super.setActionViewVisibility(!mHorizontalMode && visible);
        int visibility = visible ? VISIBLE : INVISIBLE;
        com.imay.live.utils.ViewUtil.startVisibilityAnimation(mActionRightRootView, visibility);
    }

    @Override
    public void setBottomMargin(int bottom) {
        super.setBottomMargin(bottom);
        if (mActionRightRootView != null) {
            mActionRightRootView.setPadding(mActionRightRootView.getPaddingLeft(), mActionRightRootView.getPaddingTop(), mActionRightRootView.getPaddingRight(), bottom);
        }
    }

    public void setOnLiveActionListener(OnLiveActionListener pushActionListener) {
        super.setOnActionListener(pushActionListener);
        this.mListener = pushActionListener;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
            switch (v.getId()) {
                case R.id.live_room_iv_mic:
                    mListener.onLinkMicClick();
                    break;
                case R.id.live_room_iv_game:
                    mListener.onGameClick();
                    break;
                case R.id.live_room_iv_gift:
                    mListener.onGiftClick();
                    break;
                case R.id.live_room_iv_record:
                    mListener.onRecordClick();
                    break;
                case R.id.live_room_iv_screenshot:
                    mListener.onScreenShotClick();
                    break;
                case R.id.live_room_iv_share:
                    mListener.onShareClick();
                    break;
                case R.id.live_room_iv_full:
                    mListener.onFullScreenClick();
                    break;
            }
    }

    public interface OnLiveActionListener extends OnActionListener {
        void onLinkMicClick();
        void onGameClick();
        void onGiftClick();
        void onScreenShotClick();
        void onShareClick();
        void onRecordClick();
        void onFullScreenClick();
        void onRecommendChangeClick();
        void onRecommendRoomClick(RoomInfoModel room);
    }

    public void showDiamonGuide(Activity activity, long count){
        GuideManager.showDiamonGuideDialog(activity,count,findViewById(R.id.live_room_iv_gift));
    }
}
